package docker_api

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"virtual-resource-schedule-service/api"
	"virtual-resource-schedule-service/middleware"
	"virtual-resource-schedule-service/model/docker_container"
	"virtual-resource-schedule-service/service/docker_service"
)

func CreateDockerContainer(ctx *gin.Context) {
	request := docker_container.DockerContainerRequest{}
	if err := ctx.ShouldBindJSON(&request); err != nil {
		api.HandleValidatorError(ctx, err)
	}
	zap.S().Infof("Create docker_container request: %+v", request)
	dockerService := docker_service.NewDockerContainerService(request.MachineId)
	response, err := dockerService.CreateContainer(&request)
	if err != nil {
		// @TODO
		zap.S().Errorf("Create docker_container error: %s", err.Error())
		return
	}
	resp := middleware.APIResponse{
		Code:    "00000000",
		Message: "容器创建成功",
		Data:    response,
		Success: true,
	}
	middleware.ResponseJSON(ctx, http.StatusOK, resp)
}

func StartDockerContainer(ctx *gin.Context) {
	request := docker_container.DockerContainerStartRequest{}
	if err := ctx.ShouldBindJSON(&request); err != nil {
		api.HandleValidatorError(ctx, err)
	}
	zap.S().Infof("Start docker container request: %+v", request)
	dockerService := docker_service.NewDockerContainerService(request.MachineId)
	response, err := dockerService.ContainerStart(&request)
	if err != nil {
		zap.S().Errorf("Start docker container error: %s", err.Error())
	}
	resp := middleware.APIResponse{
		Code:    "00000000",
		Message: "容器启动成功",
		Data:    response,
		Success: true,
	}
	middleware.ResponseJSON(ctx, http.StatusOK, resp)
}

func StopDockerContainer(ctx *gin.Context) {
	request := docker_container.DockerContainerStopRequest{}
	if err := ctx.ShouldBindJSON(&request); err != nil {
		api.HandleValidatorError(ctx, err)
	}
	zap.S().Infof("Stop docker container request: %+v", request)
	dockerService := docker_service.NewDockerContainerService(request.MachineId)
	response, err := dockerService.ContainerStop(&request)
	if err != nil {
		zap.S().Errorf("Stop docker container error: %s", err.Error())
	}
	resp := middleware.APIResponse{
		Code:    "00000000",
		Message: "容器停止成功",
		Data:    response,
		Success: true,
	}
	middleware.ResponseJSON(ctx, http.StatusOK, resp)
}
